package com;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;

public class MyJPanel {

    public static void main(String[] args) throws InterruptedException {


    }

    public MyJPanel() throws InterruptedException {
        JFrame jFrame = new JFrame("菜狗");

        jFrame.setSize(800, 600);
        jFrame.setLocationRelativeTo(null);
        ImageIcon imageIcon = new ImageIcon(System.getProperty("user.dir") + "/icon.jpg");
        jFrame.setIconImage(imageIcon.getImage());
        jFrame.setVisible(true);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setResizable(false);

        Thread.sleep(20);

        int x = 300;

        // 显示文字
        JLabel labelText = new JLabel("请输入表名");
        labelText.setBounds(x, 50, 200, 30);
        jFrame.add(labelText);
        jFrame.repaint();
        //创建输入框
        JTextField textField = new JTextField();
        textField.setBounds(x, 80, 200, 30);
        jFrame.add(textField);


        // 显示文字
        JLabel label1 = new JLabel("请输入包名");
        label1.setBounds(x, 150, 200, 30);
        jFrame.add(label1);
        //创建输入框
        JTextField textField1 = new JTextField();
        textField1.setBounds(x, 180, 200, 30);
        jFrame.add(textField1);


        // 显示文字
        JLabel label2 = new JLabel("请输入xml 文件夹目录");
        label2.setBounds(x, 250, 200, 30);
        jFrame.add(label2);

        //创建输入框
        JTextField textField2 = new JTextField();
        textField2.setBounds(x, 280, 200, 30);
        jFrame.add(textField2);

        // 添加按钮
        JButton button = new JButton("开始生成");
        button.setBounds(x, 450, 200, 30);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String dbTableName = textField.getText();
                String packName = textField1.getText();
                String xmlDirName = textField2.getText();

                File file = new File("../generator.properties");

                // 文件末尾追加数据
                try {
                    FileWriter fileWriter = new FileWriter(file);
                    fileWriter.append("jdbc.driverClass=com.mysql.cj.jdbc.Driver\n" +
                            "jdbc.connectionURL=jdbc:mysql://192.168.1.122:3306/realc_vr_support?characterEncoding=UTF-8&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true&allowMultiQueries=true\n" +
                            "jdbc.userId=root\n" +
                            "jdbc.password=P@ssw0rd123\n");
                    fileWriter.append("\n");
                    fileWriter.append("db.table.name=" + dbTableName + "\n");
                    fileWriter.append("pack.name=" + packName + "\n");
                    fileWriter.append("xmlDir.name=" + xmlDirName + "\n");
                    fileWriter.flush();
                    fileWriter.close();

                    Thread.sleep(1000);
                    button.disable();
                    Generator.extracted();
                    button.enable();
                } catch (Exception e1) {
                    e1.printStackTrace();
                }

            }
        });

        jFrame.add(button);

        jFrame.repaint();
    }
}
